home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / mpss / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-07-20  |  7.9 KB  |  397 lines

  1.  
  2. /*******************************************************************************
  3. ********************************************************************************
  4. ********************************************************************************
  5.  
  6. PERMISSION TO COPY THIS SOFTWARE IS HEREBY GIVEN BY THE AUTHOR PROVIDED THAT
  7. THIS LEADING MESSAGE IS INCLUDED IN ALL OF THE RELEVANT SOURCE FILES.
  8.  
  9.         P. SCHMITZ, UNIVERSITY OF KEELE, MAY 1988.
  10.  
  11.  
  12. ********************************************************************************
  13. ********************************************************************************
  14. *******************************************************************************/
  15. #include <signal.h>
  16. #include "files.h"
  17. #include "header.h"
  18.  
  19. checkmasterpidfile()
  20. {
  21. int rpid,masterpid;
  22. char string[15];
  23. FILE *ff,*fopen();
  24.  
  25. masterpid=getpid();
  26. sprintf(string,"%d",masterpid);
  27. if ((ff=fopen(MASTERPIDFILE,"r"))==NULL) 
  28.     {
  29.     /*fprintf(stdout,"main.c: Can't read masterpidfile\n");*/
  30.     die();
  31.     } else
  32.     {
  33.     fgets(string,15,ff); /*get pid*/
  34.     fclose(ff);
  35.     }
  36. sscanf(string,"%d\n",&rpid);
  37. if (rpid!=masterpid) 
  38.     {
  39.     /*fprintf(stdout,"main.c: pids not equal!!! %d %d\n",rpid,masterpid);*/
  40.     die();
  41.     }
  42. signal(SIGALRM,checkmasterpidfile);
  43. alarm(180);
  44. }
  45.  
  46.  
  47. /*the only normal way out of the program*/
  48. die()
  49. {
  50. register struct player *hh;
  51. /*fprintf(stderr,"DIE WAS CALLED.\n");*/
  52.  
  53.     hh=startlist;
  54.     while (hh!=NULL) {
  55.             if ((hh->id!=ROMID)&&(hh->id!=KLINGID)) kill(hh->pid,9);
  56.             hh=hh->next;
  57.             }
  58. /*the only normal way out of the program*/
  59.     exit(0);
  60. /*the only normal way out of the program*/
  61. }
  62. /*the only normal way out of the program*/
  63.  
  64. handler()
  65. {
  66.  noofsignals++;
  67. }
  68.  
  69. killcontrol()
  70. {
  71. int f;
  72. /*return(0); THIS MUST BE PUT IN DURING THE DEVELOPMENTAL PHASE*/
  73.  
  74. if ((f=open("/dev/tty",2))!= -1)
  75.         {
  76.         ioctl(f,TIOCNOTTY,0);
  77.         }
  78. }
  79.  
  80. main()
  81. {
  82. register struct player *cp;
  83. int f,o;
  84. register int x,y,x1,x2,y1,y2,xx1,xx2,yy1,yy2,xold,yold,xnew,ynew;
  85. register char c;
  86. register struct torpedo *torpedo;
  87. register int xx,test;
  88.     
  89. test=1;
  90. f=umask(000);
  91. killcontrol();
  92. noofsignals=0;
  93. signal(SIGINT,SIG_IGN);
  94. signal(29,handler); 
  95. signal(SIGALRM,checkmasterpidfile); 
  96. setupuniverse();
  97. storm=OFF;
  98. noofaliens=0;
  99. noofplayers=0;
  100. debug=OFF;
  101. startlist=NULL; 
  102. taillist=NULL; 
  103. tstartlist=NULL; 
  104. ttaillist=NULL;
  105. cp=NULL;
  106. putmasterpidintofile();
  107. alarm(10);
  108. for (;;)
  109. {
  110. /*main iteration loop*/
  111.     while  (cp!=NULL)
  112.     {
  113.     if ((cp->id==ROMID)||(cp->id==KLINGID)) 
  114.             {
  115.              servicealien(cp);
  116.              x=cp->xpos; y=cp->ypos;
  117.              if ((x<58)||(x>899)||(y<19)||(y>899))
  118.                 {
  119.                 if (x<58) cp->xvel=1;
  120.                 if (x>899) cp->xvel= -1;
  121.                 if (y<19) cp->yvel=1;
  122.                 if (y>899) cp->yvel= -1;
  123.                 }
  124.              goto goonlabel;
  125.              }
  126.     cp->clflg=OFF;
  127.     ioctl(cp->fd,FIONREAD,&noofchars);
  128.     if (noofchars>0) processinput(cp,&noofchars);
  129.     if ((cp->stat==5)&&(cp->flyto>=0)&&(cp->flyto<4)) auto_pilot(cp);
  130.     if (cp->stat>=9) goto goonlabel;
  131.  
  132.     /*****************PLOTTING OBJECT STARTS HERE!!!******************/
  133.  
  134.     f=cp->xvel;
  135.     o=cp->yvel;
  136.     test=1;
  137.     x=cp->xpos;
  138.     y=cp->ypos;
  139.     cp->oldxpos=x; cp->oldypos=y;
  140.     xx1=x-28; xx2=xx1+57;
  141.     yy1=y-9; yy2=yy1+19;
  142.     x+=f; y+=o;
  143.     x1=x-28; x2=x1+57;
  144.     y1=y-9; y2=y1+19;
  145.     cp->xpos=x; cp->ypos=y;
  146.     
  147.     if ((cp->stat>5)||((f==0)&&(o==0))) goto plotmoving;
  148.     
  149.     cp->chflg=FLAG;
  150.     
  151.     for (i=yy1; i<yy2; ++i)
  152.     {
  153.     if ((xp=univ[i])==NULL) continue;
  154.     do
  155.     {
  156.     if ((xx=xp->x)>=xx1)
  157.         {
  158.     l=19-(i-yy1);
  159.     if(((k=20+(xx-xx1))>19)&&(k<77)&&(l>0)&&(l<20))
  160.             {
  161.             test=0;
  162.             poscurs(cp,k,l);
  163.                  write(cp->fd,
  164.             ((l==10)&&((k<47)||(k>49)))?
  165.                     "-":((k==48)&&(l!=10))?"|":" ",1);
  166.             }
  167.     l+=o;
  168.     if(((k-=f)>19)&&(l>0)&&(k<77)&&(l<20))
  169.             {
  170.             poscurs(cp,k,l);
  171.             test=0;
  172.             write(cp->fd,&(xp->c),1);
  173.             }
  174.         } 
  175.     
  176.     if (xx>x2) 
  177.         xp=NULL;
  178.             else 
  179.         xp=xp->next_thing;
  180.  
  181.     }  while (xp!=NULL);
  182.  
  183. if (test) plotposition(cp);
  184.  
  185. /*****************************************************************************
  186.            Player plotting starts here!!!!
  187. *****************************************************************************/
  188.     
  189. plotmoving:
  190.  
  191. player=startlist;
  192.  
  193. while (player!=NULL)
  194. {
  195. if ((cp==player)||((cp->chflg==0)&&(player->chflg==0)))
  196.         {
  197.         player=player->next;
  198.         continue;
  199.         }
  200. xold=player->oldxpos;
  201. yold=player->oldypos;
  202. xnew=player->xpos;
  203. ynew=player->ypos;
  204. if (inuniv(xold+f,yold+o)<36)
  205.      {
  206. if ((player->cloakingdevice==OFF)||(player->clflg))
  207.         {
  208.         l=19-(yold-yy1);
  209.         if(((k=20+(xold-xx1))>19)&&(k<77)&&(l>0)&&(l<20))
  210.             {
  211.             poscurs(cp,k,l);
  212.             write(cp->fd,((l==10)&&((k<47)||(k>49)))?"-":((k==48)&&(l!=10))?"|":" ",1);
  213.             }
  214.         }
  215.     }
  216. if (inuniv(xnew,ynew)<36)
  217.     {
  218.     if (player->cloakingdevice==OFF)
  219.         {
  220.     if (player->stat<12)
  221.             {
  222.             l=19-(ynew-y1);
  223.             if(((k=20+(xnew-x1))>19)&&(l>0)&&(k<77)&&(l<20))
  224.                 {
  225.                 poscurs(cp,k,l);
  226.                 write(cp->fd,&(player->id),1);
  227.                 }
  228.             }
  229.         }
  230.     }
  231.     player=player->next;
  232. }
  233.  
  234. /**************************************************************************
  235.         Torpedo plotting starts here!!
  236. ***************************************************************************/
  237.  
  238. torpedo=tstartlist;
  239.  
  240. while (torpedo!=NULL)
  241. {
  242. xold=torpedo->oldxpos;
  243. yold=torpedo->oldypos;
  244. xnew=torpedo->xpos;
  245. ynew=torpedo->ypos;
  246. player=torpedo->whofrom;
  247. if (((player!=NULL)&&(cp!=player)&&(xold==player->oldxpos)&&
  248.    (yold==player->oldypos)) || ((inuniv(xold,yold)!=NULL)))
  249.     {
  250.     torpedo=torpedo->next;
  251.     continue;
  252.     }
  253.     k=20+(xold-xx1);
  254.     l=19-(yold-yy1);
  255.     if((k>19)&&(k<77)&&(l>0)&&(l<20))
  256.             {
  257.             poscurs(cp,k,l);
  258. write(cp->fd,((l==10)&&((k<47)||(k>49)))?"-":((k==48)&&(l!=10))?"|":" ",1);
  259.             }
  260.       if (torpedo->life>0)
  261.         {
  262.         l=19-(ynew-y1);
  263.         if(((k=20+(xnew-x1))>19)&&(l>0)&&(k<77)&&(l<20))
  264.             {
  265.             poscurs(cp,k,l);
  266.             write(cp->fd,&(torpedo->weapon),1);
  267.             }
  268.         }
  269.     torpedo=torpedo->next;
  270.     }
  271.         /*****************PLOTTING OBJECTS STOPS HERE!!!******************/
  272.  
  273. if ((x<58)||(x>899)||(y<19)||(y>899))
  274.     {
  275.     if (x<58) cp->xvel=1;
  276.     if (x>899) cp->xvel= -1;
  277.     if (y<19) cp->yvel=1;
  278.     if (y>899) cp->yvel= -1;
  279.     } else 
  280.     {
  281.     if ((c=inuniv(x,y))!=NULL) collision(cp,c);
  282.     if (((f!=0)||(o!=0))&&((x==450)||(y==450)))
  283.     {
  284.     j=which_sector(cp->xpos,cp->ypos,f,o);
  285.     switch (j)
  286.     {
  287.     case 1:
  288.     {
  289.     plot1(cp,"    You have just entered Federation space again.        ");
  290.     plot2(cp,"              It's nice to be home.                      ");
  291.     break;
  292.     }
  293.     case 2: 
  294.     {
  295.     plot1(cp,"You have just entered Klingon Space. Remember the klingon");
  296.     plot2(cp,"proverb : Revenge is a dish that is best served cold.....");
  297.     break;
  298.     }
  299.     case 3:
  300.     {
  301.     plot1(cp,"          You have just entered the Mutara Nebula.       ");
  302.     plot2(cp,"  Here, Shields and  Cloaking Devices will not function! ");
  303.     cp->cloakingdevice=OFF;
  304.     if (cp->shields)
  305.         {
  306.         cp->id=(char)(cp->id+32);
  307.         cp->shields=OFF;
  308.         }
  309.     plotshields(cp);
  310.     plotcloakingdevice(cp);
  311.     if (cp->stat<12) cp->chflg=FLAG;
  312.     break;
  313.     }
  314.     case 4:
  315.     {
  316.     plot1(cp,"          You have just entered the Neutral Zone.        ");
  317.     plot2(cp,"        We are now in violation of treaty Captain!!      ");
  318.     break;
  319.     }
  320.     default :
  321.     break;
  322.     } /*switch*/
  323.     }
  324.     }
  325.     
  326.     if ((storm)&&(f==-1)) storm=OFF;
  327.     /*ENERGY CALCULATION*/
  328.     i=cp->cloakingdevice;
  329.     i+=cp->shields;
  330.     j=(abs(f)>abs(o))?abs(f):abs(o);
  331.  
  332.     if (j==0)
  333.         {
  334.         if ((i==0)&&(cp->energy<cp->maxenergy)) ++cp->energy;
  335.             else goto skipenergy;
  336.         } else
  337.         {
  338.         if ((j==1) && (i==0)) goto skipenergy;
  339.         cp->energy -= (i+j);
  340.         }
  341.  
  342.     skipenergy:
  343.     
  344.     if ((cp->energy==75)&&(storm==OFF)) flare();
  345.     if (cp->energy<100) plotenergy(cp);
  346.     /*ENERGY CALCULATION*/
  347. goonlabel:
  348.     if (cp->impulsemove>0) {
  349.                 cp->impulsemove=0;
  350.                 cp->xvel=0;
  351.                 cp->yvel=0;
  352.                 }
  353.      if (cp->chflg>0) --(cp->chflg);
  354.     cp=cp->next;
  355.     } /*while cp!=NULL*/
  356. remove_finished_players();
  357. if (tstartlist!=NULL) move_torpedos();
  358. if (noofsignals>0) { 
  359.             makenewplayerandjoinhimin();
  360.                 --noofsignals;
  361.            }
  362. if ((noofplayers==1)&&(noofaliens<MAXALIENS)) makealien();
  363. cp=startlist;
  364. /*main iteration loop*/
  365. }
  366. }
  367.  
  368. remove_finished_players()
  369. {
  370. struct player *y;
  371.  
  372. y=startlist;
  373. while (y!=NULL)
  374.     {
  375.     if ((y->stat==12)&&(y->chflg<1)) 
  376.                 remove_player_from_list(y);
  377.     y=y->next;
  378.     }
  379. }
  380.  
  381. flare()
  382. {
  383. register struct player *x;
  384.  
  385. broadcast("Galactic ion storm commencing. Cloaking devices are off. ");
  386. storm=ON;
  387. x=startlist;
  388. while (x!=NULL)
  389.     {
  390.     x->cloakingdevice=OFF;
  391.     plotcloakingdevice(x);
  392.     x->chflg=FLAG;
  393.     x=x->next;
  394.     }
  395. }
  396.